package com.example.newsdaily.provider;

import com.example.newsdaily.ResourceTable;
import com.example.newsdaily.entity.ItemImage;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;

import java.util.List;

public class SwiperPageProvider extends PageSliderProvider {
    //数据实体类
    public static class DataItem{
        String mText;
        public DataItem(String txt) {
            mText = txt;
        }
    }
    // 数据源，每个页面对应list中的一项
    private List<ItemImage> list;
    private AbilitySlice abilitySlice;

    public SwiperPageProvider(List<ItemImage> list, AbilitySlice abilitySlice) {
        this.list = list;
        this.abilitySlice = abilitySlice;
    }

    @Override
    public int getCount() {
        return list.size();
    }
    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int i) {
        // 拿数据
        ItemImage itemImage = list.get(i);

        // 拿模板
        DirectionalLayout layout = (DirectionalLayout) LayoutScatter.getInstance(abilitySlice).parse(ResourceTable.Layout_page_slider_item, null,false);

        // 数据渲染模板
        Image image = (Image) layout.findComponentById(ResourceTable.Id_item_image);
        image.setPixelMap(itemImage.getIndexResourceID());

        // 渲染后的模板放入PageSlider中
        componentContainer.addComponent(layout);
        return layout;
    }
    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
        componentContainer.removeComponent((Component) o);
    }
    @Override
    public boolean isPageMatchToObject(Component component, Object o) {
        //可添加具体处理逻辑
        return true;
    }
}